﻿// 测试demo地址  \system\test\btnControl.htm
//按钮
angular.module('btnconqitk', [])
 .directive("btncon", function ($rootScope, $stateParams, T) {
     return {
         restrice: "AE",
         scope: { btnlist: "=btnObj", fun: "=fun" },
         scope: true,
         link: function (scope, element, attr) {
             //是否有内置方法，1-有，不需要在绑定，0-没有，绑定默认方法
             if (attr.fun == undefined) {
                 attr.fun = "0";
             }
             //                          console.log($rootScope.rootscopeauthority);
             //            element.addClass('btn btn-primary');
             /*加载数据源遍历*/
             //            scope.retstr.then(function (abc) {
             //                   console.log(abc.btnlist);
             //               });
             //             console.log(scope.currentTab);   //页面路由的名称

             //             console.log(scope.btnTab);   //页面路由的名称
             //                          console.log(scope.menudate);    //菜单导航集合，遍历按钮权限
             var btn_list = [];
             //通过权限，筛选对应路由按钮
             scope.btnlist_find2 = function (menus, perm) {
                 //                 console.log(menus);
                 if (menus != null) {
                     for (var i = 0; i < menus.length; i++) {
                         var menu_obj = menus[i];
                         if ($rootScope.currentTab == menu_obj.name || $rootScope.btnTab == menu_obj.name) {
                             if (menu_obj.hasOwnProperty("childroute")) {
                                 menu_obj.childroute = angular.copy(scope.btnlist_find2(menu_obj.childroute, perm));
                             }
                             if (menu_obj.hasOwnProperty("btnlist")) {
                                 if (menu_obj.btnlist != 0) {
                                     btn_list = menu_obj.btnlist;
                                 }
                             }
                         }
                     }
                 }
                 //                 return btn_list;
             }
             //遍历菜单权限
             angular.forEach(angular.copy($rootScope.rootscopeauthority), function (data, index, array) {
                 //if (scope.currentTab.indexOf(data.name) >= 0) {
                 scope.btnlist_find2(data.childroute, "");
                 //}
             });


             //             console.log(btn_list);
             //遍历按钮是否有权限
             scope.btnlist_find = function (btn_list, btnName) {
                 var btncon = false;
                 if (btn_list.length > 0) {
                     for (var i = 0; i < btn_list.length; i++) {
                         if (btn_list[i].title == btnName) {
                             btncon = true;
                         }
                     }
                 }
                 return btncon;
             }

             /***判断按钮是有权限，有权限就绑定对应是点击事件，否则就删除按钮***/
             if (attr.btncon.indexOf("btnAdd") >= 0 && attr.btncon.indexOf("btnUpd") >= 0) {//编辑                                  
                 if (scope.btnlist_find(btn_list, "Add") && scope.btnlist_find(btn_list, "Edit")) {
                     //                     if ($stateParams.routename == "index.personnelmanagement.organizational" || $stateParams.routename == "index.personnelmanagement.role" || $stateParams.routename == "index.personnelmanagement.usergroup") {
                     scope.btnlist.savefun = true;
                     if (attr.fun == "0") {
                         element.bind("click", function () {
                             scope.$apply(function () {
                                 scope.SaveFun();
                             });
                         });
                     }
                     //                     } else {
                     //                         scope.btnlist.addinfo = true;
                     //                         element.bind("click", function () {
                     //                             scope.$apply(function () {
                     //                                 scope.addinfo();
                     //                             });
                     //                         });
                     //                     }
                 } else if (scope.btnlist_find(btn_list, "Add")) {
                     scope.btnlist.addinfo = true;
                     if (attr.fun == "0") {
                         element.bind("click", function () {
                             scope.$apply(function () {
                                 scope.addinfo();
                             });
                         });
                     }
                 } else if (scope.btnlist_find(btn_list, "Edit")) {
                     scope.btnlist.editinfo = true;
                     if (attr.fun == "0") {
                         element.bind("click", function () {
                             scope.$apply(function () {
                                 scope.editinfo();
                             });
                         });
                     }
                 } else if (scope.btnlist_find(btn_list, "AddEdit")) {
                     scope.btnlist.editinfo = true;
                     scope.btnlist.addinfo = true;
                 } else {
                     element[0].disabled = true;
                 }
             } else {
                 if (attr.btncon.indexOf("btnAdd") >= 0) {//新增                     
                     if (scope.btnlist_find(btn_list, "Add")) {
                         //                     console.log("新增");
                         scope.btnlist.addinfo = true;
                         if (attr.fun == "0") {
                             element.bind("click", function () {
                                 scope.$apply(function () {
                                     scope.addinfo();
                                 });
                             }); //绑定事件
                         }
                     } else {
                         //                         element[0].disabled = true;
                         //element[0].outerHTML = "<font color='red'>无操作权限</font>";
                         element.remove(); //删除按钮
                     }
                 }

                 if (attr.btncon.indexOf("btnUpd") >= 0 || attr.btncon.indexOf("btnEdit") >= 0) {//编辑
                     if (scope.btnlist_find(btn_list, "Edit")) {
                         scope.btnlist.editinfo = true;
                         if (attr.fun == "0") {
                             element.bind("click", function () {
                                 scope.$apply(function () {
                                     scope.editinfo();
                                 });
                             });
                         }
                     } else {
                         element.remove();
                     }
                 }
             }

             if (attr.btncon.indexOf("btnDel") >= 0) {//删除
                 if (scope.btnlist_find(btn_list, "Delete")) {
                     scope.btnlist.delinfo = true;
                     if (attr.fun == "0") {
                         element.bind("click", function () {
                             scope.$apply(function () {
                                 scope.delinfo();
                             });
                         }); //绑定事件
                     }
                     //                     console.log("删除");
                 } else {
                     element.remove();
                 }
             }

             if (attr.btncon.indexOf("btnModel") >= 0) {//新增（其它类型新增按钮）                 
                 if (scope.btnlist_find(btn_list, "Add")) {
                     scope.btnlist.addmodel = true;
                     if (attr.fun == "0") {
                         element.bind("click", function () {
                             scope.$apply(function () {
                                 scope.addmodel();
                             });
                         }); //绑定事件
                     }
                     //                     console.log("删除");
                 } else {
                     element.remove();
                 }
             }
             if (attr.btncon.indexOf("btnExport") >= 0) {//导出权限               
                 if (scope.btnlist_find(btn_list, "Export")) {
                     scope.btnlist.exportinfo = true;
                     if (attr.fun == "0") {
                         element.bind("click", function () {
                             scope.$apply(function () {
                                 scope.exportmodel();
                             });
                         }); //绑定事件
                     }
                     //                     console.log("删除");
                 } else {
                     element.remove();
                 }
             }

             if (attr.btncon.indexOf("btnStatistics") >= 0) {//统计（荣誉）                 
                 if (scope.btnlist_find(btn_list, "Statistics")) {
                     scope.btnlist.statisticsinfo = true;
                     if (attr.fun == "0") {
                         element.bind("click", function () {
                             scope.$apply(function () {
                                 scope.statisticsmodel();
                             });
                         }); //绑定事件
                     }
                     //                     console.log("删除");
                 } else {
                     element.remove();
                 }
             }
             if (attr.btncon.indexOf("btnAllAdd") >= 0) {//批量新增（题库）                 
                 if (scope.btnlist_find(btn_list, "AllAdd")) {
                     scope.btnlist.alladd = true;
                     if (attr.fun == "0") {
                         element.bind("click", function () {
                             scope.$apply(function () {
                                 scope.AllAdd();
                             });
                         }); //绑定事件
                     }
                     //                     console.log("删除");
                 } else {
                     element.remove();
                 }
             }
             if (attr.btncon.indexOf("btnCannel") >= 0) {//取消（培训计划-取消计划）        
                 if (scope.btnlist_find(btn_list, "Cannel")) {
                     scope.btnlist.cannelinfo = true;
                 } else {
                     element.remove();
                 }
             }

             if (attr.btncon.indexOf("btnDistribution") >= 0) {//分配（班级--分配班级）                 
                 if (scope.btnlist_find(btn_list, "Distribution")) {
                     scope.btnlist.distribution = true;
                 } else {
                     element.remove();
                 }
             }
             if (attr.btncon.indexOf("btnSite") >= 0) {//站点（培训基地--站点）                 
                 if (scope.btnlist_find(btn_list, "Site")) {
                     scope.btnlist.siteinfo = true;
                 } else {
                     element.remove();
                 }
             }

             if (attr.btncon.indexOf("btnExamine") >= 0) {//审核                 
                 if (scope.btnlist_find(btn_list, "Examine")) {
                     scope.btnlist.examineinfo = true;
                 } else {
                     element.remove();
                 }
             }

             //班级管理指令里的菜单模块
             if (attr.btncon.indexOf("C-") >= 0) {
                 if (scope.btnlist_find(btn_list, attr.btncon)) {
                     if (attr.index != undefined&&attr.index.length>0) {
                         scope.btnlist.push(attr.index);
                     }
                     console.log(attr.btncon);
                 } else {
                     element.remove();
                 }
             }
         }
     }
 });
